<?php

namespace app\stock\controller\manage;

use think\Controller;

class Source extends Controller
{
    protected $SourceModel = null;
    protected $AdminModel = null;

    public function __construct()
    {
        parent::__construct();
        $this->SourceModel = model('business.Source');
        $this->AdminModel = model('admin.Admin');
    }

    public function index()
    {
        $adminid = $this->request->param('adminid', '', 'trim');

        $admin = $this->AdminModel->find($adminid);

        if (!$admin) {
            $this->error('该管理员不存在!');
        }

        if ($admin['status'] !== 'normal') {
            $this->error('账号已禁用!');
        }

        $sourceData = $this->SourceModel->order('id Desc')->select();

        if ($sourceData) {
            $this->success("查询成功", null, $sourceData);
        } else {
            $this->error('暂无客户来源!');
        }

    }

    public function add(){
        $adminid = $this->request->param('adminid', '', 'trim');
        $sourceName = $this->request->param('name', '', 'trim');

        $admin = $this->AdminModel->find($adminid);

        if (!$admin) {
            $this->error('管理员不存在');
        }

        if ($admin['status'] !== 'normal') {
            $this->error('当前管理员账号已被禁用');
        }

        $data = [
            'name' => $sourceName
        ];

        $result = $this->SourceModel->validate('common/business/Source')->save($data);

        if ($result === false) {
            $this->error($this->SourceModel->getError());
        } else {
            $this->success('添加成功');
        }

    }

    public function del(){
        $adminid = $this->request->param('adminid','','trim');
        $sourceid = $this->request->param('sourceid','','trim');

        $admin = $this->AdminModel->find($adminid);

        if(!$admin){
            $this->error('管理员不存在!');
        }

        if($admin['status']!=='normal'){
            $this->error('当前管理员账号已禁用!');
        }

        $source = $this->SourceModel->find($sourceid);

        if(!$source){
            $this->error('客户来源不存在!');
        }

        $result = $this->SourceModel->destroy($sourceid);

        if(!$result){
            $this->error('删除失败!');
        }else{
            $this->success('删除成功!');
        }
    }

    public function edit(){
        $adminid = $this->request->param('adminid', '', 'trim');
        $sourceName = $this->request->param('name', '', 'trim');
        $sourceid = $this->request->param('sourceid', 0, 'trim');

        $admin = $this->AdminModel->find($adminid);

        if (!$admin) {
            $this->error('管理员不存在');
        }

        if ($admin['status'] !== 'normal') {
            $this->error('当前管理员账号已被禁用');
        }

        $source = $this->SourceModel->find($sourceid);

        if (!$source) {
            $this->error('该客户来源不存在');
        }

        $data = [
            'id' => $sourceid,
            'name' => $sourceName
        ];

        $result = $this->SourceModel->isUpdate(true)->save($data);

        if(!$result){
            $this->error('编辑失败!');
        }else{
            $this->success('编辑成功!');
        }
    }

    public function info(){
        $adminid = $this->request->param('adminid', '', 'trim');
        $sourceid = $this->request->param('sourceid', 0, 'trim');

        $admin = $this->AdminModel->find($adminid);
        if(!$admin){
            $this->error('管理员不存在!');
        }

        if($admin['status']!=='normal'){
            $this->error('当前账号已禁用!');
        }

        $source = $this->SourceModel->find($sourceid);
        if(!$source){
            $this->error('该客户来源不存在!');
        }else{
            $this->success('查询成功!', null, $source);
        }


    }
}